//
//  StorageUtil.m
//  EliveClient
//
//  Created by Pierce on 2017/8/10.
//  Copyright © 2017年 Pierce. All rights reserved.
//

#import "StorageUtil.h"

@implementation StorageUtil


/**
 初始化UserDefaults
 
 @return 初始化UserDefaults
 */
+ (NSUserDefaults *)getUserDefaults
{
    return [NSUserDefaults standardUserDefaults];
}

/**
 UserDefaults存值
 
 @param key 键
 @param object 存的对象
 */
+ (void)updateUserDefaultsWithKey:(NSString *)key Object:(id)object
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:object forKey:key];
    [userDefaults synchronize];
}


/**
 UserDefaults取值
 
 @param key 键
 @return 获取值
 */
+ (id)readObjectFromUserDefaultsWithKey:(NSString *)key
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:key];
}


/**
 UserDefaults删键值对
 
 @param key 键
 */
+ (void)removeObjectFromUserDefaultsWithKey:(NSString *)key
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:key];
    [userDefaults synchronize];
}


+ (BOOL)writeToPlistWithKey:(NSString *)key Object:(id)object FileName:(NSString *)fileName
{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathArray objectAtIndex:0];
    NSString *filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.plist",fileName]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:filePath])
    {
        [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    }
    NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:filePath];
    NSMutableDictionary *tmpDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    if([tmpDic objectForKey:key])
    {
        [tmpDic removeObjectForKey:key];
    }
    [tmpDic setObject:object forKey:key];
    return [tmpDic writeToFile:filePath atomically:YES];
}
#pragma mark 通过键读取Plist的值

+ (id)readFromPlistWithKey:(NSString *)key FileName:(NSString *)fileName
{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathArray objectAtIndex:0];
    NSString *filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.plist",fileName]];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    return [dic objectForKey:key];
}

+ (BOOL)removeObjectFromPlistWithKey:(NSString *)key FileName:(NSString *)fileName
{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path =  [pathArray objectAtIndex:0];
    NSString *filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.plist",fileName]];
    NSFileManager *fileManager  = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath: filePath])
    {
        [fileManager createFileAtPath: filePath contents:nil attributes:nil];
    }
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSMutableDictionary *tmpDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    if([tmpDic objectForKey:key])
    {
        [tmpDic removeObjectForKey:key];
    }
    return [tmpDic writeToFile:filePath atomically:YES];
}



+ (BOOL)updateDictionaryToPlist:(NSDictionary *)dic FileName:(NSString *)fileName
{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathArray objectAtIndex:0];
    NSString *filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.plist",fileName]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath])
    {
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    }
    return [dic writeToFile:filePath atomically:YES];
}

+ (NSMutableDictionary *)readDictionaryFromPlistWithFileName:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [NSString stringWithFormat:@"%@/",[paths objectAtIndex:0]];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    return [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
}

+ (BOOL)updateArrayToPlist:(NSArray *)array FileName:(NSString *)fileName
{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathArray objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@.plist",path,fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath])
    {
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    }
    return [array writeToFile:filePath atomically:NO];
}

+ (NSMutableArray *)readArrayFromPlistWithFileName:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [NSString stringWithFormat:@"%@/",[paths objectAtIndex:0]];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
    return [NSMutableArray arrayWithContentsOfFile:filePath];
}

+ (BOOL)deletePlistFileWithFileName:(NSString *)fileName
{
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathArray objectAtIndex:0];
    NSString *filePath=[path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.plist",fileName]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath])
    {
        return [fileManager removeItemAtPath:filePath error:nil];
    } else {
        return NO;
    }
}



+ (BOOL)fileIsExisted:(NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *loaclFilePath = [NSString stringWithFormat:@"%@/%@",documentDirectory,[filePath lastPathComponent]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = false;
    return [fileManager fileExistsAtPath:loaclFilePath isDirectory:&isDir];
}


@end
